<template>
  <div class="home-contanier">
    <!-- 头部 -->
    <van-nav-bar class="top" fixed placeholder>
      <template #left>
        <img src="../../assets/logo.png" alt="" width="100px" height="30px" />
      </template>
      <template #right>
        <van-button
          icon="search"
          type="primary"
          class="btn-search"
          round
          size="small"
          @click="$router.push('/search')"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>
    <!-- 导航页 -->
    <van-tabs
      v-model="active"
      @click="
        list = [];
        onRefresh();
      "
    >
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell
              v-for="item in list"
              :key="item.art_id"
              :title="item.title"
              @click="
                $router.push({ path: '/details', query: { id: item.art_id } })
              "
            >
              <template #label>
                <div>
                  <van-grid :column-num="item.type">
                    <van-grid-item
                      v-for="(value, index) in item.cover.images"
                      :key="index"
                    >
                      <van-image :src="value" lazy-load />
                    </van-grid-item>
                  </van-grid>
                </div>
                <div></div>
                <span>作者：{{ item.aut_name }}</span
                >&nbsp; <span>评论：{{ item.comm_count }}</span
                >&nbsp;
                <span>时间 {{ item.pubdate | fromNow }}</span>
              </template>
            </van-cell>
          </van-list>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import { getLabelName } from "./home.js";
import { getArticlelists } from "./home.js";
import moment from "moment";
moment.locale("zh-cn");
export default {
  name: "home",
  data() {
    return {
      active: 0,
      list: [],
      loading: false,
      finished: false,
      isLoading: false,
      channels: [],
      timestamp: null,
    };
  },
  async created() {
    let res = await getLabelName();
    console.log(res);
    this.channels = res.data.data.channels;
  },
  methods: {
    async onLoad() {
      if (!this.timestamp) {
        this.timestamp = Date.now();
      }
      let res = await getArticlelists({
        channel_id: this.channels[this.active].id,
        timestamp: this.timestamp,
      });
      this.timestamp = res.data.data.pre_timestamp;
      this.list.push(...res.data.data.results);
      console.log(res);
      this.loading = false;
      if (this.list.length >= 50) {
        this.finished = true;
      }
    },
    onRefresh() {
      this.timestamp = null;
      this.finished = false;
      this.list = [];
      this.loading = true;
      this.onLoad();
      this.isLoading = false;
    },
  },
  filters: {
    fromNow(value) {
      return moment(value).fromNow();
    },
  },
};
</script>

<style lang="less">
.home-contanier {
  .van-nav-bar {
    z-index: 10;
  }
  .top {
    .van-nav-bar {
      background-color: #38bafd;
    }
    .btn-search {
      width: 100px;
      height: 30px;
      background-color: #00b2fe;
      border: 1px solid transparent;
      .van-icon-search {
        color: #fff;
      }
    }
  }
  .van-tabs__wrap {
    width: 100%;
    position: fixed;
    top: 46px;
    z-index: 10;
  }
  .van-tabs__content {
    margin-top: 44px;
  }
}
</style>
